package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.admin.mapper.AdSensitiveMapper;
import com.usian.admin.model.admin.dtos.SensitiveDto;
import com.usian.admin.model.admin.pojos.AdSensitive;
import com.usian.admin.model.common.dtos.PageResponseResult;
import com.usian.admin.model.common.dtos.ResponseResult;
import com.usian.admin.model.common.enums.AppHttpCodeEnum;
import com.usian.admin.service.AdSensitiveService;
import com.usian.base.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdSensitiveServiceImpl extends BaseServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    @Override
    public ResponseResult list(SensitiveDto dto) {
        if (dto==null)return paramError;
        Page<AdSensitive> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<AdSensitive> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())){
            wrapper.like(AdSensitive::getSensitives,dto.getName());
        }
        IPage result=page(page,wrapper);
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) result.getTotal());
        responseResult.setData(result.getRecords());
        ResponseResult responseResult1=getpageResult(dto,page,wrapper);
        return responseResult1;
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        if (adSensitive==null)return paramError;
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        if (adSensitive==null||adSensitive.getId()==null)return paramError;
        updateById(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id) {
        if (id==null)return paramError;
        AdSensitive adSensitive=getById(id);
        if (adSensitive==null)return paramError;
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
